<template>
  <header>
    <div class="banxin header_box tanxin">
      <section>欢迎来到叩丁狼积分商城</section>
      <ul>
        <li class="avater_li">
          <img width="26" :src="$store.state.UresInfo.headImg" alt="" />
          用户名：{{ $store.state.UresInfo.nickName }}
        </li>
        <li>我的积分：{{ $store.state.UresInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li v-if="$store.state.isgouwucheAndLogin" class="header_btn cart_btn">
          <img src="../assets/images/Shopping.png" alt="" />
          <span>购物车</span>
          <b>{{ $store.state.cartTotal }}</b>
        </li>
        <li class="header_btn login_btn" v-else @click="OpLogin">登录</li>
        <li class="header_btn login_btn" @click="outLogin">tuic登录</li>
        <li class="header_btn login_btn" @click="outWxLogin">解绑微信</li>
      </ul>
    </div>
  </header>
</template>

<script>
import {
  getUresInfoApi,
  WeixinLoginApi,
  UnbindWechatPhoneApi,
} from "@/request/api";
export default {
  data() {
    return {};
  },
  methods: {
    outWxLogin() {
      let token = localStorage.getItem("x-auth-token");
      if (token) {
        UnbindWechatPhoneApi().then((res) => {
          let toastObj = {
            content: res.data.data,
            icon: "success",
          };
          this.$store.dispatch("AsyncToast", toastObj);
        });
        return;
      }
      let toastObj = {
        content: "请先登录",
        icon: "danger",
      };
      this.$store.dispatch("AsyncToast", toastObj);
    },
    OpLogin() {
      this.$store.commit("changeIsOpenLogin", true);
    },
    outLogin() {
      localStorage.removeItem("x-auth-token");
      this.$router.go(0);
    },
    // 公共函数
    bindWatchFn() {
      // 如果地址栏 存在 微信的code  就去请求微信登录的接口
      // 如果存在code 才去请求 否则不做任何请求
      let code = this.$route.query.code;
      if (code) {
        WeixinLoginApi({ code }).then((res) => {
          if (res.data.code === 0) {
            let toastObj = {
              content: "登录成功",
              icon: "success",
            };
            this.$store.dispatch("AsyncToast", toastObj);
            // 关闭登录框
            this.$store.commit("changeIsOpenLogin", false);
            // 存储token
            localStorage.setItem("x-auth-token", res.data["x-auth-token"]);
            // 更新header
            this.$emit("updatakey");
            // 删除地址栏的code
            this.$router.push(this.$route.path);
          } else if (res.data.code === 400) {
            // 过期了 重新扫码
            let toastObj = {
              content: "页面过期 请重新扫描二维码",
              icon: "danger",
            };
            this.$store.dispatch("AsyncToast", toastObj);
            // 重新打开登录框
            this.$store.commit("changeIsOpenLogin", true);
          } else if (res.data.code === 407) {
            // 需要手机号绑定微信
            let toastObj = {
              content: "请使用手机号绑定微信后再使用",
              icon: "danger",
            };
            this.$store.dispatch("AsyncToast", toastObj);
            // 重新打开登录框
            this.$store.commit("changeIsOpenLogin", true);
            // 存uuid -- 临时存储 因为这个东西 很快会过期
            sessionStorage.setItem("uuid", res.data.uuid);
          } else {
            console.log("出错了");
          }
        });
      }
    },
  },
  created() {
    getUresInfoApi().then((res) => {
      if (res.data.code === 0) {
        this.$store.commit("chcartTotal", res.data.data.cartTotal);
        this.$store.commit("changeisgouwucheAndLogin", true);
        this.$store.commit("chUresInfo", res.data.data.userInfo);
      }
    });
    this.bindWatchFn();
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/base.less";
header {
  height: 40px;
  background: @black;
  color: #ffeffe;
  .header_box {
    align-items: center;
    height: 40px;
    ul {
      display: flex;
      height: 40px;
      align-items: center;
      li {
        margin-right: 20px;
        cursor: pointer;
        &.avater_li {
          display: flex;
          height: 26px;
          align-items: center;
          img {
            border-radius: 50%;
            margin-right: 8px;
          }
        }
        &.header_btn {
          width: 124px;
          height: 40px;
          background-color: @blue;
        }
        &.cart_btn {
          display: flex;
          align-items: center;
          justify-content: center;
          span {
            margin: 0 8px;
          }
          b {
            width: 22px;
            height: 22px;
            background: @orange;
            border-radius: 50%;
            text-align: center;
            line-height: 22px;
            color: #fff;
          }
        }
        &.login_btn {
          text-align: center;
          line-height: 40px;
        }
      }
    }
  }
}
</style>